package com.zhihua.interview;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhihua.interview.merge.dao.IOrderDao;
import com.zhihua.interview.merge.dao.IStockDao;
import com.zhihua.interview.merge.entity.OrderEntity;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;

@SpringBootTest
@MapperScan("com.zhihua.interview.merge.dao")
class InterviewApplicationTests2 {

    @Autowired
    public IOrderDao iOrderDao;

    @Autowired
    public IStockDao iStockDao;

//    @BeforeAll
//    public void bef(){
//        new classPathXmlApplicationContext("");
//    }


    @Test
    public void saveServiceTest() {
        String test = "{\"orderId\":\"2021-01-20-1001\",\n" +
                "\"order\" :[{\"goodsId\":\"1001\",\"name\":\"苹果手机\",\"number\" : 1},\n" +
                "{\"goodsId\":\"1002\",\"name\":\"华为手机\",\"number\" : 2},\n" +
                "{\"goodsId\":\"1003\",\"name\":\"小米手机\",\"number\" : 1},\n" +
                "{\"goodsId\":\"1004\",\"name\":\"天音手机\",\"number\" : 3},\n" +
                "{\"goodsId\":\"1002\",\"name\":\"苹果手机\",\"number\" : 1}]}";

        JSONObject jsonObject1 = JSONObject.parseObject(test);
        String orderId = (String) jsonObject1.get("orderId");
        JSONArray order = (JSONArray) jsonObject1.get("order");
        for (int i = 0; i < order.size(); i++) {
            JSONObject json = (JSONObject) order.get(i);
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setOrderId(orderId);
            orderEntity.setGoodsId(json.get("goodsId").toString());
            orderEntity.setName(json.get("name").toString());
            orderEntity.setNumber((Integer) json.get("number"));
            //插入数据库
            System.out.println("***");
            int i1 = iOrderDao.saveOrder(orderEntity);
            System.out.println("***");
        }
    }


}
